package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.MemberCollection;
import com.powernode.domain.Prod;
import com.powernode.model.Result;
import com.powernode.service.MemberCollectionService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会员收藏商品业务控制层
 */
@Api(tags = "会员收藏商品接口管理")
@RequestMapping("p/collection")
@RestController
public class MemberCollectionController {

    @Autowired
    private MemberCollectionService memberCollectionService;

    @ApiOperation("查询会员收藏商品的数量")
    @GetMapping("count")
    public Result<Integer> loadMemberCollectionProdCount() {
        String openId = AuthUtils.getLoginUserOpenId();
        Integer count = memberCollectionService.queryMemberCollectionProdCount(openId);
        return Result.success(count);
    }

//    p/collection/prods?current=1&size=10
    @ApiOperation("分页查询会员收藏商品列表")
    @GetMapping("prods")
    public Result<Page<Prod>> loadMemberCollectionProdPage(Page<Prod> page) {
        String openId = AuthUtils.getLoginUserOpenId();
        page = memberCollectionService.queryMemberCollectionProdPage(page,openId);
        return Result.success(page);
    }

    //    p/collection/isCollection?prodId=101
    @ApiOperation("查询会员收藏商品状态")
    @GetMapping("isCollection")
    public Result<Boolean> loadMemberIsCollectionProd(@RequestParam Long prodId) {
        String openId = AuthUtils.getLoginUserOpenId();
        long count = memberCollectionService.count(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId)
                .eq(MemberCollection::getProdId, prodId)
        );
        return Result.success(1==count);
    }

//    p/collection/addOrCancel
    @ApiOperation("添加或取消收藏商品")
    @PostMapping("addOrCancel")
    public Result<String> addOrCancel(@RequestBody Long prodId) {
        String openId = AuthUtils.getLoginUserOpenId();
        Integer count = memberCollectionService.addOrCancelProd(openId,prodId);
        return Result.handle(count>0);
    }
}
